package cn.edu.scau.cmi.wuzhenxin.hibernate.service;

import java.util.List;

import cn.edu.scau.cmi.wuzhenxin.hibernate.dao.StudentDAOByHibernate;
import cn.edu.scau.cmi.wuzhenxin.hibernate.domain.Student;
import cn.edu.scau.cmi.wuzhenxin.hibernate.domain.Teacher;

public class StudentService {

	private StudentDAOByHibernate studentDAOByHibernate = new StudentDAOByHibernate();

	public List<Student> findAll(){
		return studentDAOByHibernate.findAll();
	}
	
	public Student findById(int id) {
		return studentDAOByHibernate.findById(id);
	}
	
	public boolean saveStudent(String name, Teacher tutor) {
		Student student = new Student();
		student.setName(name);
		student.setTutor(tutor);
		return studentDAOByHibernate.save(student);
	}
	
	public boolean modifyStudent(int id, String name, Teacher tutor) {
		Student student = studentDAOByHibernate.findById(id);
		if(student == null) {
			return false;
		}
		if(name.length()>0) {
			student.setName(name);
		}
		if(tutor!=null) {
			student.setTutor(tutor);
		}
		return studentDAOByHibernate.updateById(student);
	}
	
	public boolean deleteStudent(Integer id, String name) {
		Student student = new Student();
		student.setId(id);
		student.setName(name);
		return studentDAOByHibernate.deleteById(student);
	}
	
}
